package cn.dcw.pethome.org.service.impl;

import cn.dcw.pethome.basic.exception.MyException;
import cn.dcw.pethome.basic.service.impl.BaseServiceImpl;
import cn.dcw.pethome.basic.util.MD5Utils;
import cn.dcw.pethome.basic.util.StrUtils;
import cn.dcw.pethome.org.domain.Employee;
import cn.dcw.pethome.org.domain.Shop;
import cn.dcw.pethome.org.dto.ShopDto;
import cn.dcw.pethome.org.mapper.EmployeeMapper;
import cn.dcw.pethome.org.mapper.ShopMapper;
import cn.dcw.pethome.org.service.IShopService;
import cn.dcw.pethome.user.domain.LoginInfo;
import cn.dcw.pethome.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/*
门店入驻
 */
@Service

public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    //加载门店mapper
    @Autowired
    private ShopMapper mapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    //添加门店
    @Override
    @Transactional
    public void add(Shop shop) {
        mapper.add(shop);
       this.update(shop);
    }

 /*   *//**
     * 店铺入驻
     * @param shop
     *//*
    @Override
    public void settledIn(Shop shop) {
        //1.判断两次输入的密码是否一致
        Employee admin = shop.getAdmin();//前端传递过来封装好了对象
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            //密码不一致抛出异常  MyException-自定义异常
            throw new MyException("密码不一致，请检查···");
        }
        //2.用户名 和电话  邮箱 一般都是可以当作账号登入，需要判断是否重复
        Employee emp = employeeMapper.findByName(admin.getUsername());
        if(emp!=null){//用户名已经存在
            throw new MyException("用户名重复，请重新输入···");
        }
        //3.先添加员工，返回自增长主键
        employeeMapper.add(admin);
        //4.在添加商铺shop信息，返回自增长主键
        mapper.add(shop);
        //5.将shop的自增长主键设置到员工shop_id
        admin.setShop_id(shop.getId());
        //6.修改员工表的信息
        employeeMapper.update(admin);
    }*/

    /**
     * 店铺入驻
     * @param shop
     */
    @Override
    public void settledIn(ShopDto shopDto) {
        //1.判断两次输入的密码是否一致
        if(!shopDto.getPassword().equals(shopDto.getComfirmPassword())){
            //密码不一致抛出异常  MyException-自定义异常
            throw new MyException("密码不一致，请检查···");
        }
        //2.用户名 和电话  邮箱 一般都是可以当作账号登入，需要判断是否重复
        Employee emp = employeeMapper.findByName(shopDto.getUsername());
        if(emp!=null){//用户名已经存在
            throw new MyException("用户名重复，请重新输入···");
        }
        //创建一个员工对象
        Employee employee = new Employee();
        BeanUtils.copyProperties(shopDto,employee );//bean属性复制对象

        //将输入的密码加密加盐处理
        String salt = StrUtils.getComplexRandomString(32);//盐值
        String password = MD5Utils.encrypByMd5(employee.getPassword() + salt);//储存到数据库的密码
        //将盐值和加密后的密码储存到数据库
        employee.setSalt(salt);
        employee.setPassword(password);

        //创建一个loginInfo对象
        LoginInfo loginInfo = new LoginInfo();
        //将员工对象拷贝到loginInfo
        BeanUtils.copyProperties(employee, loginInfo);
        loginInfo.setType(shopDto.getType());
        //将登入的数据添加进去,返回自增长主键设置到员工对象
        loginInfoMapper.add(loginInfo);
        employee.setLogininfo_id(loginInfo.getId());
        //3.先添加员工，返回自增长主键
        employeeMapper.add(employee);
        //4.在添加商铺shop信息，返回自增长主键
        Shop shop = new Shop();
        BeanUtils.copyProperties(shopDto,shop);
        shop.setAdmin(employee);
        mapper.add(shop);
        //5.将shop的自增长主键设置到员工shop_id
        employee.setShop_id(shop.getId());
        //6.修改员工表的信息
        employeeMapper.update(employee);
    }


}
